package com.imist.cake.service;

import com.imist.cake.common.MybatisUtils;
import com.imist.cake.entity.Category;
import com.imist.cake.mapper.CategoryMapper;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;

public class CategoryService {

    public List<Category> getCategories(){
        SqlSession sqlSession = MybatisUtils.openSession();
        CategoryMapper categoryMapper = sqlSession.getMapper(CategoryMapper.class);
        try {
            return categoryMapper.getCategories();
        }finally {
            sqlSession.close();
        }
    }

    /**
     * 添加蛋糕分类
     * @param category
     */
    public void addCategory(Category category) {
        Date now = new Date();
        category.setCreateTime(now);
        category.setUpdateTime(now);
        SqlSession sqlSession = MybatisUtils.openSession();
        CategoryMapper categoryMapper = sqlSession.getMapper(CategoryMapper.class);
        try {
           categoryMapper.addCategory(category);
           sqlSession.commit();
        }finally {
            sqlSession.close();
        }
    }
}
